package com.park.mer.service.order.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.park.comm.ParkEnum;
import com.park.comm.ResultEnum;
import com.park.dao.mapper.order.ParkOrderInfoMapper;
import com.park.dao.mapper.park.ParkInfoMapper;
import com.park.entity.mer.MerUserInfoLoginDto;
import com.park.entity.order.MerOrderItem;
import com.park.entity.order.MerOrderStartEndDto;
import com.park.entity.order.ParkOrderInfo;
import com.park.entity.page.PageInfoParm;
import com.park.entity.park.ParkInfo;
import com.park.exception.CommException;
import com.park.mer.service.order.MerOrderService;
import com.park.utils.distance.LongLatDistanceUtil;
import com.park.utils.fee.FeeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @title: MerOrderServiceImpl
 * @projectName park
 * @description: 商户订单管理 商户支付管理
 * @date 2020/5/415:40
 */
@Service(value = "merOrderService")
public class MerOrderServiceImpl implements MerOrderService {

    @Autowired
    private ParkInfoMapper parkInfoMapper;

    @Autowired
    private ParkOrderInfoMapper parkOrderInfoMapper;

    /**
 　　* @description: 查询范围内的所有的订单 未开始和已结束的
 　　* @param [merUserInfoLoginDto]
 　　* @return {@link PageInfo}
 　　* @throws
 　　* @author ZhangHaiWen
 　　* @version 2.0
 　　* @date 2020/5/4 15:58
 　　*/
    public PageInfo orderList(PageInfoParm<MerUserInfoLoginDto> pageInfoParm,Integer userId) {
        MerUserInfoLoginDto param = pageInfoParm.getParam();
        Double merUserLat = param.getMerUserLat();
        Double merUserLong = param.getMerUserLong();
        // 查询范围内的停车场
        List<ParkInfo> parkInfos = parkInfoMapper.selectAll();
        List<Integer> parkIds = parkInfoList(merUserLong, merUserLat, parkInfos, 1000d);
        PageHelper.startPage(pageInfoParm.getPageNum(),pageInfoParm.getPageSize());
        List<MerOrderItem> merOrderItems =  parkOrderInfoMapper.getOrderList(parkIds);
        PageInfo<MerOrderItem> pageInfo = new PageInfo<MerOrderItem>(merOrderItems);
        return pageInfo;
    }
    /**
 　　* @description: 查询范围内的所有的支付订单 未支付 和已经支付的
 　　* @param [merUserInfoLoginDto]
 　　* @return {@link PageInfo}
 　　* @throws
 　　* @author ZhangHaiWen
 　　* @version 2.0
 　　* @date 2020/5/4 15:58
 　　*/
    public PageInfo payList(PageInfoParm<MerUserInfoLoginDto> pageInfoParm,Integer userId) {
        // 查询范围内的停车场
        MerUserInfoLoginDto param = pageInfoParm.getParam();
        Double merUserLat = param.getMerUserLat();
        Double merUserLong = param.getMerUserLong();
        List<ParkInfo> parkInfos = parkInfoMapper.selectAll();
        List<Integer> parkIds = parkInfoList(merUserLong, merUserLat, parkInfos, 1000d);
        PageHelper.startPage(pageInfoParm.getPageNum(),pageInfoParm.getPageSize());
        List<MerOrderItem> merOrderItems =  parkOrderInfoMapper.getPayList(parkIds);
        PageInfo<MerOrderItem> pageInfo = new PageInfo<MerOrderItem>(merOrderItems);
        return pageInfo;
    }

    /**
     　　* @description: 点击开始按钮开始计时
     　　* @param [merUserInfoLoginDto]
     　　* @return
     　　* @throws
     　　* @author ZhangHaiWen
     　　* @version 2.0
     　　* @date 2020/5/4 19:26
     　　*/
    public void onStart(MerOrderStartEndDto merUserInfoLoginDto) {
        Integer merUserId = merUserInfoLoginDto.getMerUserId();
        Integer orderId = merUserInfoLoginDto.getOrderId();
        ParkOrderInfo parkOrderInfo = parkOrderInfoMapper.selectByPrimaryKey(orderId);
        if(ObjectUtil.isEmpty(parkOrderInfo)){
            throw new CommException(ResultEnum.MER_ORDER_NULL_ERROR);
        }
        long timestamp = System.currentTimeMillis();
        parkOrderInfo.setInTimeStamp(timestamp);
        parkOrderInfo.setTimeStamp(timestamp);
        parkOrderInfo.setOrderStatus(ParkEnum.PARK_STATUS_ING.status);
        parkOrderInfo.setOrderPayStatus(ParkEnum.ORDER_PAY_STATUS_UNPAY.status);
        parkOrderInfo.setInMerUserId(merUserId);
        parkOrderInfoMapper.updateByPrimaryKeySelective(parkOrderInfo);
    }

    /**
     　　* @description: 点击结束按钮结束时间，计算收费
     　　* @param [merUserInfoLoginDto]
     　　* @return
     　　* @throws
     　　* @author ZhangHaiWen
     　　* @version 2.0
     　　* @date 2020/5/4 19:26
     　　*/
    public void onEnd(MerOrderStartEndDto merUserInfoLoginDto) {
        Integer merUserId = merUserInfoLoginDto.getMerUserId();
        Integer orderId = merUserInfoLoginDto.getOrderId();
        ParkOrderInfo parkOrderInfo = parkOrderInfoMapper.selectByPrimaryKey(orderId);
        if(ObjectUtil.isEmpty(parkOrderInfo)){
            throw new CommException(ResultEnum.MER_ORDER_NULL_ERROR);
        }
        Integer parkId = parkOrderInfo.getParkId();
        ParkInfo parkInfo = parkInfoMapper.selectByPrimaryKey(parkId);
        //单位时间 费用
        BigDecimal parkHourFee = parkInfo.getParkHourFee();
        //人工费
        BigDecimal parkLabour = parkInfo.getParkLabour();
        long endTime = System.currentTimeMillis();
        parkOrderInfo.setOutTimeStamp(endTime);
        //更新时间
        parkOrderInfo.setTimeStamp(endTime);
        parkOrderInfo.setOrderStatus(ParkEnum.PARK_STATUS_END.status);
        parkOrderInfo.setOrderPayStatus(ParkEnum.ORDER_PAY_STATUS_UNPAY.status);
        parkOrderInfo.setOutMerUserId(merUserId);
        BigDecimal orderTotal = FeeUtils.feeCalculat(parkOrderInfo.getInTimeStamp(), endTime, parkLabour);
        parkOrderInfo.setOrderTotal(orderTotal);
        parkOrderInfo.setOrderPrice(parkHourFee);
        parkOrderInfoMapper.updateByPrimaryKeySelective(parkOrderInfo);
    }


    //查找距离范围内所有的停车场
    private List<Integer> parkInfoList(Double userLong,Double userLat,List<ParkInfo> parkInfos,double distance){
        List<Integer> parkIdlist = new ArrayList<Integer>();
        //判断是否存在可以停车的距离
        for (ParkInfo parkInfo : parkInfos) {
            Double parkLat = parkInfo.getParkLat();
            Double parkLong = parkInfo.getParkLong();
            double dis = LongLatDistanceUtil.distance(userLat, userLong, parkLat, parkLong);
            if(dis<=distance){
                parkIdlist.add(parkInfo.getId());
            }
        }
        return parkIdlist;
    }

}
